package com.example.service;


import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.example.entities.Partner;
import com.example.entities.Project;
import com.example.entities.User;

public class PartnerHandler {
	
	public void create(Partner partner)
	{
		EntityManager em = EMFService.get().createEntityManager();
		em.getTransaction().begin();
		em.persist(partner);
		
		em.getTransaction().commit();
		em.close();
		
	}
	
	public List<Partner> getAllPartner()
	
	{
		EntityManager em = EMFService.get().createEntityManager();
		em.getTransaction().begin();
		
		Query query = em.createQuery("SELECT p FROM Partner p");
		List<Partner> partner = query.getResultList();
		
		em.getTransaction().commit();
		em.close();
		
		return partner;
	}
	
	public int deleteByPartnerName(String partnerName)
	{
		EntityManager em = EMFService.get().createEntityManager();
		em.getTransaction().begin();
		
		Query q = em.createQuery("DELETE FROM Partner p WHERE p.name = :u");
		q.setParameter("u", partnerName );
		int deleted = q.executeUpdate();
		
		em.getTransaction().commit();
		em.close();
		
		return deleted;

	}

}
